var userid = null;
$(function() {
	var commodityId = getQueryStr('commodityId');
	var integral = getQueryStr('integral');
	$.ajax({
		type : "post",
		url : "/dingding-web/userMallAction!getCommodityInfo.action",
		dataType : "jsonp",
		async : false,
		jsonp : "callback",
		jsonpCallback : "success_jsonp",
		data : {
			commodityId : commodityId
		},
		success : function(data) {
			var tpl = document.getElementById('priceInfoTpl').innerHTML;
			data.responseBody.integral = integral;
			var html = juicer(tpl, data.responseBody);

			$(".body-content").html(html);

			// 初始化滑动
			$("#carousel-generic").carousel('cycle');

			// 绑定滑动事件
			$('#carousel-generic').bind('swipeleft', function() {
				$(this).carousel('next');
			});
			$('#carousel-generic').bind('swiperight', function() {
				$(this).carousel('prev');
			});
			$('#buyItemModal').on('show.bs.modal', buyItemModalcenter());
			$('#showSuccess').on('show.bs.modal', showSuccesscenter());
			$('#showFaild').on('show.bs.modal', showFaildcenter());
		}
	});
	
	// 保养券
	$(document).on("click", ".jump", function() {
		window.location.href = "http://wx.pdyulu.com/active/active.aspx?service=027b4329-7db1-4414-bd49-f0f142518df3"
		return false;
	})
});

// 模态框居中
function buyItemModalcenter() {
	$('#buyItemModal').each(
			function(i) {
				var $clone = $(this).clone().css('display', 'block').appendTo(
						'body');
				var top = Math.round(($clone.height() - $clone.find(
						'.modal-content').height()) / 2);
				top = top > 0 ? top : 0;
				$clone.remove();
				$(this).find('.modal-content').css("margin-top", top);
			});

}

function showSuccesscenter() {
	$('#showSuccess').each(
			function(i) {
				var $clone = $(this).clone().css('display', 'block').appendTo(
						'body');
				var top = Math.round(($clone.height() - $clone.find(
						'.modal-content').height()) / 2);
				top = top > 0 ? top : 0;
				$clone.remove();
				$(this).find('.modal-content').css("margin-top", top);
			});
}

function showFaildcenter() {
	$('#showFaild').each(
			function(i) {
				var $clone = $(this).clone().css('display', 'block').appendTo(
						'body');
				var top = Math.round(($clone.height() - $clone.find(
						'.modal-content').height()) / 2);
				top = top > 0 ? top : 0;
				$clone.remove();
				$(this).find('.modal-content').css("margin-top", top);
			});
}

function buyItem() {

	var commodityId = getQueryStr('commodityId');
	var userId = getQueryStr('userId');

	userid = userId;

	$("buyItemNow").css("disabled", "disabled");
	$("#buyItemModal").modal('hide');

	// 判断有无网络
	netWorkIsOk();

	$.ajax({
		type : "post",
		url : "/dingding-web/userMallAction!buyItem.action",
		dataType : "jsonp",
		async : false,
		jsonp : "callback",
		jsonpCallback : "success_jsonp",
		data : {
			commodityId : commodityId,
			userId : userId
		},
		success : function(data) {
			if (data.code == 100) {
				$("#showSuccess").modal('show');
			} else {
				$("#showFaild").modal('show');
			}
		}
	});
}

function goRefresh() {
	if (isIOS()) {
		var userId = getQueryStr('userId');
		window.location.href = baseUrl
				+ "activityAction!pageRedirectProxy.action?bizType=mall&bizId="
				+ userId;
	} else {
		history.go(-1);
	}

}